<?php

class Database_connections {

	/**
	 * 
	 * @return PDO|false if connection was established pdo returned else false
	 */
	static function get_connection () {
		$conn_array = Database_connections::load_database_yaml();
		if ($conn_array) {
			$conn_array = $conn_array[DATABASE_MODE];
			try { return new PDO("{$conn_array['adapter']}:host={$conn_array['host']};dbname={$conn_array['database']}", "{$conn_array['username']}", "{$conn_array['password']}", array(PDO::ATTR_PERSISTENT => true)); }
			catch (PDOException $e) {
				die('unable to open db connection: '.$e->getMessage());
			}
		}
		echo 'db connection could not be established';
		return false;
	}

	/**
	 *
	 * @return array|false if database.yml was loaded and parsed correctly an array of the file is returned else false
	 */
	private static function load_database_yaml() {
		$db_yaml_file = CONFIG . 'database.yml';
		if (file_exists($db_yaml_file)) { return Spyc::YAMLLoad($db_yaml_file); }
		echo $db_yaml_file, ' file not found';
		return false;
	}

}


?>
